﻿#pragma once
#include"RBTree.h"

namespace zkz
{
	template<class K, class V>
	class map
	{
		struct MapKeyOfT 
		{
			const K& operator()(const pair<const K, V>& kv)
			{
				return kv.first;
			}
		};

	public:
		typedef typename RBTree<K, pair<const K,V>, MapKeyOfT>::Iterator  iterator;
		typedef typename RBTree<K, pair<const K, V>, MapKeyOfT>::const_Iterator const_iterator;

		iterator begin()
		{
			return _t.begin();
		}
		iterator end()
		{
			return _t.end();
		}
		const_iterator begin()const
		{
			return _t.begin();
		}
		const_iterator end()const
		{
			return _t.end();
		}
		const_iterator cbegin()const { return begin(); }
		const_iterator cend()const { return end(); }

		pair<iterator,bool> insert(const pair<K,V>& data)
		{
			return _t.Insert(data);
		}
		iterator find(const K& key)
		{
			return _t.Find(key);
		}
		V& operator[](const K& key)
		{
			pair<iterator,bool> ret = insert({key,V()});
			return ret.first->second;
		}
	private:
		RBTree<K, pair<const K, V>, MapKeyOfT> _t;
	};

	void test_map()
	{
		map<string, string> dict;
		dict.insert({ "sort", "排序" });
		dict.insert({ "left", "左边" });
		dict.insert({ "right", "右边" });
		dict["left"] = "左边，剩余";
		dict["insert"] = "插入";
		dict["string"];
		map<string, string>::iterator it = dict.begin();
		while (it != dict.end())
		{
			//it->first="5";
			it->second += 'x';
			cout << it->first << ":" << it->second << endl;
			++it;
		} 
		cout << endl;
	
		for (auto& [x,y] : dict)
		{
			cout << x << ":" << y << endl;
		}
	}
}